// 合并数组
const sumTopic = function (arr) {
    return arr.reduce((pre, cur) => pre.concat(cur.value), []);
};
export default {
    namespaced: true,
    state: {
        examIndex: 0,
        examRulesList: [],
        examList: [],
        examAnswerList: [], // 用户考试的答案
        currentPlayerKey: "", // 当前播放器的key
    },
    getters: {
        getTopicId: (state) => {
            if (uni.$u.test.empty(state.examList)) return 0;
            const topic = state.examList.find(
                (item, index) => index == state.examIndex
            );
            if (topic) {
                return topic.topic_id;
            }
            return 0;
        },
    },
    mutations: {
        init(state, data) {
            state.examIndex = 0;
            if (uni.$u.test.empty(data)) {
                state.examIndex = 0;
                state.examList = [];
                state.examRulesList = [];
                state.examAnswerList = [];
            } else {
                state.examList = sumTopic(data);
                state.examRulesList = data;
                // 设置试卷答案的数据模型
                data.forEach((item) => {
                    state.examAnswerList.push({
                        part_id: item.id,
                        data: [],
                    });
                });
            }
        },
        // 上一题
        prev(state) {
            state.examIndex -= 1;
        },
        // 下一题
        next(state) {
            state.examIndex += 1;
        },
        // 点击答题卡跳转到对应的题
        change(state, index) {
            state.examIndex = index;
        },
        // 保存正确的答案
        changeCorrectAnswer(state, { id, answer }) {
            if (!uni.$u.test.empty(state.examList)) {
                const topic = state.examList.find(
                    (item) => item.topic_id == id
                );
                topic.correct_answer = answer; // 正确的答案
            }
        },
        // 保存选择的答案
        changeCurrentAnswer(state, data) {
            // 修改答案状态
            let topic = null;
            if (!uni.$u.test.empty(data.pid)) {
                topic = state.examList.find(
                    (item) => item.topic_id == data.pid
                );
                if (topic) {
                    topic.has_answered = 1; // 已答
                }
            } else {
                topic = state.examList.find((item) => item.topic_id == data.id);
                if (topic) {
                    topic.has_answered = uni.$u.test.empty(data.answer) ? 0 : 1; // 已答
                }
            }

            // 设置答题数据
            state.examAnswerList.forEach((ele) => {
                if (ele.part_id == data.part_id) {
                    const list = ele.data.find(
                        (item) => item.topic_id == data.id
                    );
                    if (list) {
                        list.answer = data.answer;
                        list.topic_id = data.id;
                        list.topic_level = data.topic_level;
                        list.question_type = data.question_type;
                    } else {
                        ele.data.push({
                            answer: data.answer,
                            topic_id: data.id,
                            topic_level: data.topic_level,
                            question_type: data.question_type,
                        });
                    }
                }
            });
        },
        player(state, val) {
            state.currentPlayerKey = val;
        },
    },
};
